/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.fifa.informacion_mundialista.controllers;
import com.fifa.filters.SecurityFilter;
import com.stefanini.informacion_mundialista.ejbs.UserManager;
import com.stefanini.informacion_mundialista_data.entities.Usuario;
import java.io.Serializable;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.servlet.http.HttpSession;
/**
 *
 * @author gbuitrago
 */
@Named(value="loginController")
@RequestScoped
public class LoginController implements Serializable{
    private static final long serialVersionUID = 1L;
    
    private String usuario;
    private String password;
    
    @EJB
    private UserManager userManager;
    
   
    public void setUsuario(String Usuario) {
        this.usuario = Usuario;
    }

    public void setPassword(String Password) {
        this.password = Password;
    }

    public String getUsuario() {
        return usuario;
    }

    public String getPassword() {
        return password;
    }
    
    public void login(){
        Usuario user = userManager.verificarLogin(usuario, password);
        
        FacesContext ctx = FacesContext.getCurrentInstance();
        
        if(user == null){
            ctx.addMessage(null, 
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error",
                            "Usuario o contraseña inválida"));
        }
        else{
            HttpSession session = (HttpSession) ctx.getExternalContext().getSession(true);
            session.setAttribute(SecurityFilter.LOGGED_USER, user.getUsername());
            session.setAttribute(SecurityFilter.IS_LOGGED_IN, Boolean.TRUE);

            try {
                String url = ResourceBundle.getBundle("com.fifa.urls").getString("index.url");
                //ctx.getExternalContext().redirect("/informacion_mundialista/pages/index.xhtml");
                ctx.getExternalContext().redirect(url);
            } catch (Exception e) {
                Logger.getLogger(LoginController.class.getName()).log(Level.SEVERE, "Error al redireccionar", e);
            }
        }
    }
    
    
    public void logout() {
        FacesContext ctx = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) ctx.getExternalContext().getSession(false);
        session.invalidate();
        
        try {
            ctx.getExternalContext().redirect("/informacion_mundialista/pages/index.xhtml");
        } catch (Exception e) {
            Logger.getLogger(LoginController.class.getName()).log(Level.SEVERE, null, e);
        }
        
    }
    
        public void changeLanguage(){
        if(FacesContext.getCurrentInstance()
                .getViewRoot().getLocale().equals(new Locale("es"))){
            FacesContext.getCurrentInstance()
                    .getViewRoot().setLocale(Locale.ENGLISH);
        } else {
            FacesContext.getCurrentInstance()
                    .getViewRoot().setLocale(new Locale("es"));
        }
    }
}
